package com.xci.platform.auth.controller;

import com.xci.platform.auth.entity.DocEntity;
import com.xci.platform.auth.service.DocService;
import com.xci.platform.annotation.Authorize;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.StringHelper;
import com.xci.platform.upload.IUploadFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 系统文档控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/doc")
public class DocController extends AuthBaseController {
    /**
     * 文档服务
     */
    @Resource
    protected DocService docService;

    /**
     * 文件上传
     */
    @Resource
    private IUploadFile uploadFile;

    /**
     * 文件上传参数
     */
    @Resource
    private MultipartProperties multipartProperties;

    //region 页面视图

    /**
     * 文档首页
     */
    @GetMapping
    @Authorize(code = "SystemDoc")
    public String index() {
        return "auth/doc/index";
    }

    /**
     * 文档新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemDoc.Create")
    public String create(String categoryId, ModelMap map) {
        createMark(map);
        DocEntity entity = new DocEntity();
        entity.setCategoryId(categoryId);
        entity.setId(StringHelper.guid());
        entity.setStatus(1);
        entity.setDocFormat(1);
        map.put("entity", entity);
        return "auth/doc/edit";
    }

    /**
     * 文档编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemDoc.Edit")
    public String edit(String id, ModelMap map) {
        DocEntity entity = docService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/doc/edit";
    }

    /**
     * 文档详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        DocEntity entity = docService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/doc/details";
    }

    //endregion

    //region 数据处理

    /**
     * 文档表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemDoc.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(docService.queryPage(params));
    }

    /**
     * 保存新增文档
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemDoc.Create")
    public BoolMessage createSave(@ModelAttribute DocEntity entity) {
        return docService.save(entity, true);
    }

    /**
     * 保存修改文档
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemDoc.Create")
    public BoolMessage editSave(@ModelAttribute DocEntity entity) {
        return docService.save(entity, false);
    }

    /**
     * 修改文档启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemDoc.Status")
    public BoolMessage status(String id, Integer status) {
        return docService.updateStatus(id, status);
    }

    /**
     * 删除文档
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemDoc.Delete")
    public BoolMessage delete(String ids) {
        return docService.delete(StringHelper.splitToArray(ids));
    }

    //endregion
}